// @ts-nocheck
/* eslint-disable */
interface TableDataRef {
  value: {
    $el: HTMLElement;
    $refs: {
      scrollBarRef: {
        wrapRef: HTMLElement
      }
    }
  }
}

export const dragTable = (tableDataRef: TableDataRef) => {
  let mouseFlag = false,
  mouseStart = 0,
  startX = 0,
  tableBody: HTMLElement | null = null;
  if( tableDataRef.value==null)
	  return
  // 鼠标按下事件，即鼠标按下但未释放的动作
  tableDataRef.value.$el.addEventListener('mousedown', (e: MouseEvent) => {
    tableBody = tableDataRef.value.$refs.scrollBarRef.wrapRef;
    setMouseFlag(true, tableBody);
    mouseStart = e.clientX;
    startX = tableBody.scrollLeft;
    document.addEventListener('dragstart', handleDragStart);
  });
  
  // 鼠标释放事件，即鼠标释放的动作
  tableDataRef.value.$el.addEventListener('mouseup', () => {
    setMouseFlag(false, tableBody);
    document.removeEventListener('dragstart', handleDragStart);
  });

  // 鼠标移动事件，即鼠标移动的动作
  tableDataRef.value.$el.addEventListener('mousemove', (e: MouseEvent) => {
    if (mouseFlag) {
      let offset = e.clientX - mouseStart;
      (tableBody as HTMLElement).scrollLeft = startX - offset;
    }
  });

  // 鼠标离开事件，即鼠标离开的动作
  tableDataRef.value.$el.addEventListener('mouseleave', (e: MouseEvent) => {
    handleDragStart((e as any))
  });

  // 检测到禁止光标手势

  const handleDragStart = (e: DragEvent) => {
    e.preventDefault();
    setMouseFlag(false, tableBody);
  };

  const setMouseFlag = (flag: boolean, tableBody: HTMLElement | null) => {
    mouseFlag = flag;
    if (tableBody) {
      tableBody.style.cursor = flag? 'col-resize' : 'default';
    }
  };
}


// 定义过滤条件类型
export interface TableFilter {
  column: string;      // 要过滤的列名
  value: any;          // 过滤值
  type?: string;       // 过滤类型：contains, equals, startsWith, endsWith, greaterThan, lessThan 等
}

/**
 * 通用表格数据过滤函数
 * @param data 原始表格数据
 * @param filters 过滤条件配置数组
 * @returns 过滤后的表格数据
 */
export const filterTableData = (data: any[], filters: TableFilter[]): any[] => {
  if (!data || !filters || filters.length === 0) {
    return data;
  }
  
  return data.filter(item => {
    // 所有过滤条件都需要满足
    return filters.every(filter => {
      const { column, value, type = 'contains' } = filter;
      const itemValue = item[column];
      
      // 如果表单项值或过滤值为空，跳过该条件
      if (itemValue === null || itemValue === undefined || value === null || value === undefined || value === '') {
        return true;
      }
      
      // 转换为字符串进行比较
      const strItemValue = String(itemValue).toLowerCase();
      const strValue = String(value).toLowerCase();
      
      switch (type) {
        case 'contains':
          return strItemValue.includes(strValue);
        case 'equals':
          return strItemValue === strValue;
        case 'startsWith':
          return strItemValue.startsWith(strValue);
        case 'endsWith':
          return strItemValue.endsWith(strValue);
        case 'greaterThan':
          return Number(itemValue) > Number(value);
        case 'lessThan':
          return Number(itemValue) < Number(value);
        case 'greaterThanOrEqual':
          return Number(itemValue) >= Number(value);
        case 'lessThanOrEqual':
          return Number(itemValue) <= Number(value);
        default:
          return strItemValue.includes(strValue);
      }
    });
  });
};

